/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaAlterarLei.java
 *
 * Created on 14/04/2011, 23:44:51
 */

package gui;

import dao.DAOCadastroLei;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import modelo.Lei;
import util.Paginador;
import util.PaginadorListener;

/**
 *
 * @author Jaedson
 */
public class JanelaAlterarLei extends javax.swing.JFrame implements UI, PaginadorListener {


    /** Creates new form JanelaAlterarLei */
    public JanelaAlterarLei() {
        initComponents();
        setLocationRelativeTo(null);
        setTitle("Alterar Lei");
    }

    public void carregarDados() {
      ((Paginador) paginador).mostrouPagina();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    botaoAlterar = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    tabela = new javax.swing.JTable();
    paginador = new Paginador(new DAOCadastroLei());

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Janela Alterar Lei");
    setResizable(false);

    botaoAlterar.setText("Alterar");
    botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        botaoAlterarActionPerformed(evt);
      }
    });

    tabela.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {

      }
    ));
    tabela.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        tabelaMouseClicked(evt);
      }
    });
    jScrollPane1.setViewportView(tabela);

    ((Paginador) paginador).addPaginadorListener(this);
    paginador.setPreferredSize(new java.awt.Dimension(206, 29));

    javax.swing.GroupLayout paginadorLayout = new javax.swing.GroupLayout(paginador);
    paginador.setLayout(paginadorLayout);
    paginadorLayout.setHorizontalGroup(
      paginadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 314, Short.MAX_VALUE)
    );
    paginadorLayout.setVerticalGroup(
      paginadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 41, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
          .addGroup(layout.createSequentialGroup()
            .addComponent(paginador, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(26, 26, 26)
            .addComponent(botaoAlterar))
          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(botaoAlterar)
          .addComponent(paginador, javax.swing.GroupLayout.PREFERRED_SIZE, 41, Short.MAX_VALUE))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
        int index = getTabela().getSelectedRow();
        Lei l = new Lei();
        l.setNumeroLei(tabela.getValueAt(index, 0).toString());
        l.setProcesso(tabela.getValueAt(index, 1).toString());
        l.setEmenta(tabela.getValueAt(index, 2).toString());
        l.setTexto(tabela.getValueAt(index, 3).toString());
        String s = (tabela.getValueAt(index, 4).toString());
        l.setId(Long.parseLong(s));
        janelaCadastrarLei.setarCampos(l);
        janelaCadastrarLei.setVisible(true);
    }//GEN-LAST:event_botaoAlterarActionPerformed

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
//
    }//GEN-LAST:event_tabelaMouseClicked

    /**
    * @param args the command line arguments
    */

    //private NegocioCadastroLei negocio;
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton botaoAlterar;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JPanel paginador;
  private javax.swing.JTable tabela;
  // End of variables declaration//GEN-END:variables

    private ControladorAlterarLei controlador;
    private JanelaCadastrarLei janelaCadastrarLei;

    public void setControlador(Controlador c){
        controlador = (ControladorAlterarLei) c;
    }

    public void setJanelaCadastrarLei(JanelaCadastrarLei jcl){
        janelaCadastrarLei = jcl;
    }

    public void setControladorAlterarLei(ControladorAlterarLei c){
        controlador = c;
    }

    public Controlador getControlador(){
        return controlador;
    }

    public void exibirMensagem(String mensagem) {
        JOptionPane.showMessageDialog(this, mensagem);
    }

    public void exibirMensagemErro(String mensagem) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mostrarObjeto(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mostrarObjetos(List l) {
       montarTabela((ArrayList<Lei>) l);
    }

  @Override
  public void mudaPagina(List paginas) {
    mostrarObjetos(paginas);
  }

    public class ModeloTabela extends AbstractTableModel{

        ArrayList<Lei> leis;

        ModeloTabela(ArrayList<Lei> p){
            leis = p;
        }

        public String getColumnName(int column){
            if(column == 0) return "Nº Lei";
            else if(column == 1) return "Processo";
            else if(column == 2) return "Ementa";
            else if(column == 3) return "Texto";
            else if(column == 4) return "ID";
            return null;
        }

        public int getColumnCount() {
            return 5;
        }

        public int getRowCount() {
            return leis.size();
        }

        public Object getValueAt(int row, int col) {
            Lei p = leis.get(row);
            if (col==0) return p.getNumeroLei();
            else if (col==1) return p.getProcesso();
            else if (col==2) return p.getEmenta();
            else if (col==3) return p.getTexto();
            else if (col==4) return p.getId();
            return null;
        }


    }

    public void montarTabela(ArrayList<Lei> leis){
        tabela.setModel(new ModeloTabela(leis));
    }

    public JTable getTabela(){
        return tabela;
    }



}
